//
//  VideoChatViewController.m
//  Agora iOS Tutorial Objective-C
//
//  Created by James Fang on 7/15/16.
//  Copyright © 2016 Agora.io. All rights reserved.
//

#import "InterviewChatViewController.h"
#import "KeyCenter.h"
#import "InterviewJobInfoView.h"
#import "JobDetailViewController.h"
#import "PreviewResumeViewController.h"

@interface InterviewChatViewController ()<InterviewJobInfoViewDelegate>

@property (strong, nonatomic) AgoraRtcEngineKit *agoraKit;          // Tutorial Step 1
@property (weak, nonatomic) IBOutlet UIView *localVideo;            // Tutorial Step 3
@property (weak, nonatomic) IBOutlet UIView *remoteVideo;           // Tutorial Step 5
@property (weak, nonatomic) IBOutlet UIView *controlButtons;        // Tutorial Step 8
@property (weak, nonatomic) IBOutlet UIImageView *remoteVideoMutedIndicator;
@property (weak, nonatomic) IBOutlet UIImageView *localVideoMutedBg;
@property (weak, nonatomic) IBOutlet UIImageView *localVideoMutedIndicator;

@end

@implementation InterviewChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.view.backgroundColor = UISeparatedColorRGB;
    [self setupButtons];            // Tutorial Step 8
    [self hideVideoMuted];          // Tutorial Step 10
    [self initializeAgoraEngine];   // Tutorial Step 1
    [self setupVideo];              // Tutorial Step 2
    [self setupLocalVideo];         // Tutorial Step 3
    [self joinChannel];             // Tutorial Step 4
	[self addJobInfoView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addJobInfoView {
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	InterviewJobInfoView *jobInfoView = [[InterviewJobInfoView alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT*0.64, SCREEN_WIDTH-20, 164)];
	[jobInfoView viewWithTag:1].layer.cornerRadius = 20;
	
	if (role == 5 || role == 6) {
		NSDictionary *param = @{
								@"resume_id":self.resumeId
								};
		[[NetworkTool sharedTool] requestWithName:@"获取面试直播间简历信息" URLString:APIGetResume parameters:param method:@"POST" callBack:^(id responseObject) {
			NSDictionary *dict = responseObject[@"data"];
			[jobInfoView.image sd_setImageWithURL:[NSURL URLWithString:dict[@"head_img"]]];
			jobInfoView.titleText.text = @"求职者";
			jobInfoView.name.text = dict[@"name"];
			jobInfoView.position.text = dict[@"category_title"];
			jobInfoView.jobWant.text = dict[@"category_title"];
			jobInfoView.workExp.text = dict[@"work_life"];
		}];

	}
	else {
		NSDictionary *param = @{@"position_id" : self.positionId};
		[[NetworkTool sharedTool] requestWithName:@"获取面试直播间职位信息" URLString:APIJobDetail parameters:param method:@"POST" callBack:^(id responseObject) {
			NSDictionary *dict = responseObject[@"data"];
			[jobInfoView.image sd_setImageWithURL:[NSURL URLWithString:dict[@"company_logo"]]];
			jobInfoView.titleText.text = dict[@"company_title"];
			jobInfoView.name.text = dict[@"publisher"];
			jobInfoView.position.text = dict[@"position"];
			jobInfoView.jobWant.text = dict[@"category_title"];
			jobInfoView.workExp.text = dict[@"work_life"];
		}];
	}
	jobInfoView.delegate = self;
	[self.view addSubview:jobInfoView];
}

- (void)showMore
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 5 || role == 6) {
		PreviewResumeViewController *vc = [[PreviewResumeViewController alloc] init];
		vc.shouldShowInviteBtn = NO;
		vc.resumeId = self.resumeId;
		vc.isFromLive = YES;
		[self addChild:vc inRect:CGRectMake(20, 40, SCREEN_WIDTH-40, SCREEN_HEIGHT-80)];
	}
	else {
		
		JobDetailViewController *vc = [[JobDetailViewController alloc] init];
		vc.position_id = self.positionId;
		vc.shouldHideDeliverBtn = YES;
		vc.isFromLive = YES;
		[self addChild:vc inRect:CGRectMake(20, 40, SCREEN_WIDTH-40, SCREEN_HEIGHT-80)];
	}
}

// Tutorial Step 1
- (void)initializeAgoraEngine {
    self.agoraKit = [AgoraRtcEngineKit sharedEngineWithAppId:[KeyCenter AppId] delegate:self];
}

// Tutorial Step 2
- (void)setupVideo {
    [self.agoraKit enableVideo];
    // Default mode is disableVideo
    
    [self.agoraKit setVideoProfile:AgoraVideoProfileLandscape360P swapWidthAndHeight: false];
    // Default video profile is 360P
}

// Tutorial Step 3
- (void)setupLocalVideo {
    AgoraRtcVideoCanvas *videoCanvas = [[AgoraRtcVideoCanvas alloc] init];
    videoCanvas.uid = 0;
    // UID = 0 means we let Agora pick a UID for us
    
    videoCanvas.view = self.localVideo;
    videoCanvas.renderMode = AgoraVideoRenderModeAdaptive;
    [self.agoraKit setupLocalVideo:videoCanvas];
    // Bind local video stream to view
}

// Tutorial Step 4
- (void)joinChannel {
    [self.agoraKit joinChannelByToken:nil channelId:self.chatRoomId info:nil uid:0 joinSuccess:^(NSString *channel, NSUInteger uid, NSInteger elapsed) {
        // Join channel "demoChannel1"
        [self.agoraKit setEnableSpeakerphone:YES];
        [UIApplication sharedApplication].idleTimerDisabled = YES;
    }];
    // The UID database is maintained by your app to track which users joined which channels. If not assigned (or set to 0), the SDK will allocate one and returns it in joinSuccessBlock callback. The App needs to record and maintain the returned value as the SDK does not maintain it.
}

// Tutorial Step 5
- (void)rtcEngine:(AgoraRtcEngineKit *)engine firstRemoteVideoDecodedOfUid:(NSUInteger)uid size: (CGSize)size elapsed:(NSInteger)elapsed {
    if (self.remoteVideo.hidden)
        self.remoteVideo.hidden = false;
    AgoraRtcVideoCanvas *videoCanvas = [[AgoraRtcVideoCanvas alloc] init];
    videoCanvas.uid = uid;
    // Since we are making a simple 1:1 video chat app, for simplicity sake, we are not storing the UIDs. You could use a mechanism such as an array to store the UIDs in a channel.
    
    videoCanvas.view = self.remoteVideo;
    videoCanvas.renderMode = AgoraVideoRenderModeAdaptive;
    [self.agoraKit setupRemoteVideo:videoCanvas];
    // Bind remote video stream to view
    
    if (self.remoteVideo.hidden)
        self.remoteVideo.hidden = false;
}

// Tutorial Step 6
- (IBAction)hangUpButton:(UIButton *)sender {
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定结束面试？" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[self leaveChannel];
	}]];
	[self presentViewController:alert animated:YES completion:nil];
}

- (void)leaveChannel {
    [self.agoraKit leaveChannel:^(AgoraChannelStats *stat) {
        [self hideControlButtons];     // Tutorial Step 8
        [UIApplication sharedApplication].idleTimerDisabled = NO;
        [self.remoteVideo removeFromSuperview];
        [self.localVideo removeFromSuperview];
        self.agoraKit = nil;
		NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
		if(role == 5 || role == 6) {
			[[NetworkTool sharedTool] requestWithName:@"面试结束" URLString:APIEndInterview parameters:@{@"interview_id":self.interviewId} method:@"POST" callBack:^(id responseObject) {
				[self dismissViewControllerAnimated:YES completion:nil];
			}];
		}
		else {
			[self dismissViewControllerAnimated:YES completion:nil];
		}
    }];
}

// Tutorial Step 7
- (void)rtcEngine:(AgoraRtcEngineKit *)engine didOfflineOfUid:(NSUInteger)uid reason:(AgoraUserOfflineReason)reason {
    self.remoteVideo.hidden = true;
}

// Tutorial Step 8
- (void)setupButtons {
    [self performSelector:@selector(hideControlButtons) withObject:nil afterDelay:3];
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(remoteVideoTapped:)];
    [self.view addGestureRecognizer:tapGestureRecognizer];
    self.view.userInteractionEnabled = true;
}

- (void)hideControlButtons {
    self.controlButtons.hidden = true;
}

- (void)remoteVideoTapped:(UITapGestureRecognizer *)recognizer {
    if (self.controlButtons.hidden) {
        self.controlButtons.hidden = false;
        [self performSelector:@selector(hideControlButtons) withObject:nil afterDelay:3];
    }
}

- (void)resetHideButtonsTimer {
    [InterviewChatViewController cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(hideControlButtons) withObject:nil afterDelay:3];
}

// Tutorial Step 9
- (IBAction)didClickMuteButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self.agoraKit muteLocalAudioStream:sender.selected];
    [self resetHideButtonsTimer];
}

// Tutorial Step 10
- (IBAction)didClickVideoMuteButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self.agoraKit muteLocalVideoStream:sender.selected];
    self.localVideo.hidden = sender.selected;
    self.localVideoMutedBg.hidden = !sender.selected;
    self.localVideoMutedIndicator.hidden = !sender.selected;
    [self resetHideButtonsTimer];
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine didVideoMuted:(BOOL)muted byUid:(NSUInteger)uid {
    self.remoteVideo.hidden = muted;
    self.remoteVideoMutedIndicator.hidden = !muted;
}

- (void) hideVideoMuted {
    self.remoteVideoMutedIndicator.hidden = true;
    self.localVideoMutedBg.hidden = true;
    self.localVideoMutedIndicator.hidden = true;
}

// Tutorial Step 11
- (IBAction)didClickSwitchCameraButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self.agoraKit switchCamera];
    [self resetHideButtonsTimer];
}


@end


